package com.shiliao.app.activity.interlocution.presenter;

import android.text.TextUtils;

import com.shiliao.app.activity.interlocution.service.ICommonProblemDetailService;
import com.shiliao.app.activity.interlocution.service.impl.CommonProblemDetailImpl;
import com.shiliao.app.activity.interlocution.view.CommonProblemDetailView;
import com.shiliao.app.utils.YMServerUtil;
import com.yimei.framework.mvp.presenter.IBasePresenter;
import com.yimei.framework.network.ICallBack;
import com.yimei.framework.util.LogUtil;
import com.yimei.framework.util.ParseHelper;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by yinzhiqiang on 2017/10/10.
 */

public class CommonProblemDetailPresenter extends IBasePresenter<ICommonProblemDetailService> {
    private CommonProblemDetailView commonProblemDetailView;
    private ICommonProblemDetailService commonProblemDetailService;

    public CommonProblemDetailPresenter(CommonProblemDetailView commonProblemDetailView) {
        this.commonProblemDetailView = commonProblemDetailView;
        commonProblemDetailService = new CommonProblemDetailImpl();
        setService(commonProblemDetailService);
    }

    public void requestQuestionAnswer(String userId, String questionId, String answer) {//提交发言
        Map map = new HashMap();
        LogUtil.e("yzq", questionId);
        if (!TextUtils.isEmpty(userId)) {
            map.put("user_id", userId);
        }
        if (!TextUtils.isEmpty(questionId)) {
            map.put("question_id", questionId);
        }
        if (!TextUtils.isEmpty(answer)) {
            map.put("answer", answer);
        }
        LogUtil.e("yzq", "userid:" + userId + "..question_id:" + questionId + "...answer:" + answer);
        commonProblemDetailService.requestQuestionAnswer(YMServerUtil.ACTION_ANSWER_QUESTION, map, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, Object result) {
                LogUtil.e("yzq", result.toString());
                commonProblemDetailView.commentSuccess();
            }

            @Override
            public void onFail(int statusCode, Object result) {

            }
        });
    }

    public void requestCollectionQuestion(String userId, String questionId) {//收藏问题
        Map map = new HashMap();
        if (!TextUtils.isEmpty(userId)) {
            map.put("user_id", userId);
        }
        if (!TextUtils.isEmpty(questionId)) {
            map.put("question_id", questionId);
        }
        commonProblemDetailService.requestCollectionQuestion(YMServerUtil.ACTION_CONNECTION_QUESTION, map, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, Object result) {
                LogUtil.e("yzq", result.toString());
                commonProblemDetailView.setCollectionIcon();
            }

            @Override
            public void onFail(int statusCode, Object result) {

            }
        });
    }

    public void requestCancelQuestion(String userId, String questionId) {//取消收藏问题接口
        Map map = new HashMap();
        if (!TextUtils.isEmpty(userId)) {
            map.put("user_id", userId);
        }
        if (!TextUtils.isEmpty(questionId)) {
            map.put("question_id", questionId);
        }
        commonProblemDetailService.requestCollectionQuestion(YMServerUtil.ACTION_CANCEL_QUESTION, map, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, Object result) {
                commonProblemDetailView.setNoCollection();
                LogUtil.e("yzq", result.toString());
            }

            @Override
            public void onFail(int statusCode, Object result) {

            }
        });
    }

    public void requestCheckQuestion(String userId, String questionId) {//检查问题是否被收藏
        Map map = new HashMap();
        if (!TextUtils.isEmpty(userId)) {
            map.put("user_id", userId);
        }
        if (!TextUtils.isEmpty(questionId)) {
            map.put("question_id", questionId);
        }
        commonProblemDetailService.requestCollectionQuestion(YMServerUtil.ACTION_CHECK_QUESTION, map, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, Object result) {
                LogUtil.e("yzq", result.toString());
                String stored = ParseHelper.getString(result.toString(), "stored");
                commonProblemDetailView.isCollectionQuestion(stored);
            }

            @Override
            public void onFail(int statusCode, Object result) {

            }
        });
    }

    public void requestShareContent(String questionId, String userId) {
        Map map = new HashMap();
        if (!TextUtils.isEmpty(userId)) {
            map.put("user_id", userId);
        }
        if (!TextUtils.isEmpty(questionId)) {
            map.put("question_id", questionId);
        }
        commonProblemDetailService.requestCancelQuestion(YMServerUtil.ACTION_QUESTION_SHARE,
                map, new ICallBack() {
                    @Override
                    public void onSuccess(int statusCode, Object result) {
                        LogUtil.e("yzq", result.toString());
                        String sharTitle = ParseHelper.getString(result.toString(), "title");
                        String sharDescription = ParseHelper.getString(result.toString(), "description");
                        String sharPicurl = ParseHelper.getString(result.toString(), "pic_url");
                        String sharurl = ParseHelper.getString(result.toString(), "url");
                        commonProblemDetailView.shareContent(sharTitle, sharPicurl, sharurl, sharDescription);
                    }

                    @Override
                    public void onFail(int statusCode, Object result) {

                    }
                });
    }
}
